package com.ecoolex.hotel.saas.platform.util;

import static com.ecoolex.hotel.saas.Constant.STORE_BRAND_PARAM_KEY;
import static com.ecoolex.hotel.saas.Constant.STORE_PARAM_KEY;

import java.util.List;

import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.platform.PlatformMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.platform.PlatformMemberRoleuser;
import com.ecoolex.hotel.saas.pojo.StoreLicense;
import com.google.common.collect.Lists;

public class StoreLicenseUtil {

	/**
	 * 获取商户的权限参数
	 * 
	 * @param user
	 * @param authKey
	 * @return
	 */
	public static final StoreLicense generate(PlatformMemberAuthority user, String authKey) {
		StoreLicense license = new StoreLicense();
		license.setBrandIdList(Lists.newArrayList());
		license.setStoreIdList(Lists.newArrayList());

		List<String> brandList = user.allRoleParamByKey(STORE_BRAND_PARAM_KEY);
		for (String brandId : brandList) {
			// 查询属于参数的角色
			PlatformMemberRoleuser role = user.roleByParam(STORE_BRAND_PARAM_KEY, brandId);
			// 判断是否具有权限
			if (Check.notNull(role) && role.auth(authKey)) {
				license.getBrandIdList().add(Integer.valueOf(brandId));
			}
		}

		List<String> storeList = user.allRoleParamByKey(STORE_PARAM_KEY);
		for (String storeId : storeList) {
			// 查询属于参数的角色
			PlatformMemberRoleuser role = user.roleByParam(STORE_PARAM_KEY, storeId);
			// 判断是否具有权限
			if (Check.notNull(role) && role.auth(authKey)) {
				license.getStoreIdList().add(Integer.valueOf(storeId));
			}
		}

		return license;
	}
}
